#include "../logs/log_sys.h"

#include <unistd.h>
#include <string>

// // 向文件中输出日志
enum class TimeGap{
    GAP_SECOND,
    GAP_MINUTE,
    GAP_HOUR,
    GAP_MDAY,
};

class RollByTimeSink : public log_sys::LogSink{
public:
    // 构造函数传入文件的路径名，并打开这个文件
    RollByTimeSink(const std::string& basename, TimeGap gap_type)
        :_basename(basename)
    {
        switch (gap_type)
        {
        case TimeGap::GAP_SECOND:
            _timegap = 1;
            break;
        case TimeGap::GAP_MINUTE:
            _timegap = 60;
            break;
        case TimeGap::GAP_HOUR:
            _timegap = 3600;
            break;
        case TimeGap::GAP_MDAY:
            _timegap = 3600 * 24;
            break;
        }

        _curgap = _timegap == 1 ? log_sys::util::Date::now() : log_sys::util::Date::now() % _timegap;
        std::string filename = creatNewFile();
        log_sys::util::File::createDirectory(log_sys::util::File::path(filename));
        // 往文件中添加写日志，
        // 打开文件的方式是以二进制打开，
        // 追加文本的方式添加到文件结尾的方式是追加
        _ofs.open(filename, std::ios::binary | std::ios::app);
        assert(_ofs.is_open());
    }
    void log(const char* data, size_t len){
        time_t cur = log_sys::util::Date::now();
        if ((cur % _timegap) != _curgap)
        {
            _ofs.close();
            std::string filename = creatNewFile();
            _ofs.open(filename, std::ios::binary | std::ios::app);
            assert(_ofs.is_open());
        }
        
        _ofs.write(data, len);
        assert(_ofs.good());
    }

private:
    std::string creatNewFile(){
        // 获取系统时间，以时间来创建创建新的日志文件的名称
        time_t t = log_sys::util::Date::now();
        struct tm lt;
        localtime_r(&t, &lt);
        std::stringstream filename;
        filename << _basename << lt.tm_year + 1900 << lt.tm_mon + 1
                    << lt.tm_mday << lt.tm_hour << lt.tm_min << lt.tm_sec << ".log";
        return filename.str();
    }

private:
    std::string _basename;
    std::ofstream _ofs;
    size_t _curgap; // 当前是第几个时间段
    size_t _timegap; // 时间段的大小
};

void test_log(const std::string& name){
    INFO("%s", "测试开始");
    log_sys::Logger::ptr logger = log_sys::LoggerManager::getInstance().getLogger(name);
    logger->debug("%s", "测试日志");
    logger->info("%s", "测试日志");
    logger->warn("%s", "测试日志");
    logger->error("%s", "测试日志");
    logger->fatal("%s", "测试日志");
    INFO("%s", "测试完毕");

}

int main()
{
    // 测试重构后按照时间打印日志
    std::unique_ptr<log_sys::loggerBuilder> builder(new log_sys::GlobalLoggerBuilder());
    builder->NameBuilder("asy");
    builder->LevelBuilder(log_sys::Log_level::Value::WARN);
    builder->FormatBuilder("%c:[%f:%l]%m%n");
    builder->TypeBuilder(log_sys::LoggerType::ASYNC_LOGGER);
    // builder->ChangeUnsafeAsyncBuilder(); 
    builder->SinkBuilder<RollByTimeSink>("./logfile/roll-async.log-by-time", TimeGap::GAP_SECOND);
    log_sys::Logger::ptr logger = builder->build();

    size_t cur = log_sys::util::Date::now();
    while (log_sys::util::Date::now() < cur + 5)
    {
        logger->fatal("这是一条测试日志");
        usleep(1000);
    }
    

    return 0;
}
